package com.omg.business.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.omg.server.framework.dao.FacadeDAO;
import com.omg.server.util.CommonUtil;
import com.omg.util.ApplicationContextUtil;

public class RedirectServlet extends HttpServlet {
	private FacadeDAO facadeDao = (FacadeDAO)ApplicationContextUtil.getBean("facadeDAO");		

	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		String param = req.getRequestURI();
		if (CommonUtil.isNotEmpty(param)){
			int index = param.lastIndexOf("/");
			String bizSeq = null;
			if (index > -1){
				bizSeq = param.substring(index+1);
				if (CommonUtil.isNotEmpty(bizSeq)){
					List<String> targetUrl = facadeDao.find_iBatis("omg.view.queryTargetUrlBySeq", Integer.valueOf(bizSeq));
					if (CommonUtil.isNotEmpty(targetUrl)){
						String finalUrl = targetUrl.get(0);
						resp.sendRedirect(finalUrl);
					}
				}
				
			}
		}
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		doGet(req, resp);
	}
}
